package io.github.wx.task;

import com.jfinal.kit.LogKit;
import com.jfinal.plugin.redis.Redis;
import com.xiaoleilu.hutool.http.HttpUtil;
import com.xiaoleilu.hutool.json.JSONObject;
import com.xiaoleilu.hutool.json.JSONUtil;
import io.github.wx.dao.model.BizNews;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by jie on 2017/4/19.
 * get readhub data
 */

public class SpiderNewsTask implements Runnable {
    private static final String REDIS_NEWS_KEY = "newsIds";
    private static final int PAGESIZE = 10;

    private void getDatas() {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("pageSize", PAGESIZE);
        String url = "https://api.readhub.me/news";
        String reuslt = HttpUtil.get(url, params);
        JSONObject jsonObject = JSONUtil.parseObj(reuslt);
        List<JSONObject> list = (List<JSONObject>) jsonObject.get("data");
        for (JSONObject newsObject : list) {
            Integer fieldId = (Integer) newsObject.get("id");
            boolean haveId = Redis.use().hexists(REDIS_NEWS_KEY, fieldId);
            if (!haveId) {
                BizNews news = newsObject.toBean(BizNews.class);
                news.save();
                LogKit.info("新增一条新闻:" + news.getTitle());
            }
            Redis.use().hset(REDIS_NEWS_KEY, fieldId, new Date());
        }
    }

    public void run() {
        getDatas();
    }

}
